package utility;

import java.awt.*;
/**
 *Timer responsible for recording the time spent. 
 *It also provide a method - public int remainTime()- to get the Score.
 *It can not count time by itself, must combine with TimerPanel to do so.
 * @author s1010937
 */
public class Timer{
	/**
	 *total time and time spent.
	 */
    private int timeLimitation; // in second
    /**
     *Color instance for background color. 
     */
    private Color backgroundColor;
    /**
     *Color instance for foreground color. 
     */
    private Color foregroundColor;
    private Color defaultForegroundColor;
    /**
     *Color instance for message color. 
     */
    private Color stringColor;
    /**
     *Color instance for timer border color. 
     */
    private Color borderColor;
    /**
     *Timer width.
     */
    public int width;
    /**
     *Timer height.
     */
    public int height;
    /**
     *rectangle that represent time spent. 
     */
    private int foregroundWidth;
    /**
     *the width of foreground decrease each time.
     */
    private int divider;
    /**
     *displayed massage for user. 
     */
    private String msg;
    /**
     *is the game over? 
     */
    public boolean gameover;
    /**
     *pause the Timer(stop  counting).
     */
    public boolean pause;
    
    private int counter = 0;
    private int x;
    private int y;
    
    /**
     *Defines an Timer object with a default fields. 
     */
    public Timer(int x, int y){
        
    	msg = "Time left: ";
    	gameover = false;
    	pause = false;
        timeLimitation = 60;
        width = 600;
        widthAdjust();
//System.out.println(width);
        foregroundWidth = width;
        height = 13;
        divider = (int)(width/timeLimitation);
        backgroundColor = new Color(211, 211, 211);
        foregroundColor = new Color(0, 191, 255, 50);
        defaultForegroundColor = new Color(foregroundColor.getRed(), foregroundColor.getGreen(), foregroundColor.getBlue(), foregroundColor.getAlpha());
        stringColor = new Color(139, 137, 137);
        borderColor = Color.BLACK;
        this.x = 0;
        this.y = y - height;
    }
    
    /**
     *user specified Timer constructor.
     */
    public Timer(int width, int height, int timeLimitation,String msg, Color msgColor, Color backgroundColor, Color frontColor, Color boundColor){
    	
    }
    private void widthAdjust(){
    	if(width % timeLimitation != 0){
    		width = width - width % timeLimitation;
    	}
    }
    /**
     * Timer constructor with specified massage.
     */
    public Timer(String msg){
    	this.msg = msg;
    }
    /**
     * getter method
     * @return the width(int) of the Timer
     */
    public int getWidth(){
    	return width;
    }
    /**
     * getter method
     * @return the height(int) of the Timer
     */
    public int getHeight(){
    	return height;
    }
    /**
     *Timer draw itself
     * @param g a painting brush to paint the Timer
     */
    public void draw(Graphics g){
    	
    	Color c = g.getColor();
	        
        g.setColor(backgroundColor);
        g.fillRect(x, y, width, height);
        g.setColor(stringColor);
        g.setFont(new Font("",Font.BOLD,16));/*modified for larger font size*/
        g.drawString(msg+timeLimitation+"second",x+1, y+12);
        decideForegroundSizeAndColor();
    	g.setColor(foregroundColor);
    	g.fillRect(x, y, foregroundWidth, height);
    	g.setColor(borderColor);
    	g.drawRect(x, y, width, height);
    	g.setColor(c);
    	
    }
    
    private void resetForegroundColor(){
    	foregroundColor = new Color(defaultForegroundColor.getRed(), defaultForegroundColor.getGreen(), defaultForegroundColor.getBlue(), defaultForegroundColor.getAlpha());
    }
    private void decideForegroundSizeAndColor(){
    	if(timeLimitation > 0 && !pause){	        
	        if(counter == 9){
	        	timeLimitation--;
	        	foregroundWidth -= divider;
	        	counter = 0;
	        }else{
	        	counter++;
	        }
    	}else{
    		pause = true;
    	}
    	
    }
    /**
     *stop the Timer
     */
    public void stop(){
    	pause = true;
    }
    public void resume(){
    	pause = false;
    }
    public boolean isPause(){
    	return pause;
    }
    /**
     *restart the timer 
     * @param timeLimitation
     */
    public void restart(int timeLimitation){
    	foregroundWidth = width;
    	pause = false;
        this.timeLimitation = timeLimitation;
    }
    /**
     *get the time spent/score 
     * @return the time(int) that user spent
     */
    public int remainTime(){
    	return timeLimitation;
    }
    /**
     *restart the timer
     */
    public void restart(){
        restart(60);
    }
    
    
}

